# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from ..items import ToscrapebookItem
from scrapy.loader import ItemLoader

class BooksSpider(scrapy.Spider):
    name = 'books2'
    allowed_domains = ['books.toscrape.com']
    start_urls = ['http://books.toscrape.com/']

    def parse(self, response):
        book_urls = response.xpath("//article//div[@class='image_container']/a/@href").extract()
        for book_url in book_urls:
            if book_url:
                book_url = response.urljoin(book_url)
                yield scrapy.Request(book_url,callback=self.book_parse)

        #取下一页的url地址
        nex_url = response.xpath("//li[@class='next']/a/@href").extract_first()
        if nex_url:
            nex_url = response.urljoin(nex_url)
            yield scrapy.Request(nex_url, callback=self.parse)

    def book_parse(self,response):
        load_book = ItemLoader(item = ToscrapebookItem(), response = response)
        load_book.add_xpath('bookname',"//h1/text()")
        load_book.add_xpath('bookprice',"//table/tr[3]/td/text()")
        load_book.add_xpath('bookstock',"//*[re:match(text(),'stock \([0-9].')]/text()")
        yield load_book.load_item()